package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.DiscountPolicy;
import com.ericom.service.GenericManager;

public class DiscountPolicyAction extends BaseAction {

	private GenericManager<DiscountPolicy, Long> discountPolicyManager;
	private List discountPolicys;
	private DiscountPolicy discountPolicy;
	private Long id;

	public DiscountPolicy getDiscountPolicy() {
		return discountPolicy;
	}

	public void setDiscountPolicy(DiscountPolicy discountPolicy) {
		this.discountPolicy = discountPolicy;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setDiscountPolicyManager(
			GenericManager<DiscountPolicy, Long> discountPolicyManager) {
		this.discountPolicyManager = discountPolicyManager;
	}

	public String list() {
		discountPolicys = discountPolicyManager.getAll();
		return SUCCESS;
	}

	public List getDiscountPolicys() {
		return discountPolicys;
	}

	public String delete() {
		discountPolicyManager.remove(discountPolicy.getId());
		this.saveMessage(getText("discountPolicy.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			discountPolicy = discountPolicyManager.get(id);
		} else {
			discountPolicy = new DiscountPolicy();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (discountPolicy.getId() == null);
		discountPolicy = discountPolicyManager.save(discountPolicy);

		String key = (isNew) ? "discountPolicy.added"
				: "discountPolicy.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
